/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.AccountDAO;
import dao.CategoryDAO;
import dao.ManufacturerDAO;
import dao.ProductDAO;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import pojo.Account;
import pojo.Category;
import pojo.Manufacturer;
import pojo.Product;
import ultilities.FS_Ulti;

/**
 *
 * @author Nero
 */
@Controller
public class Admin {

    @RequestMapping("/admin")
    public ModelAndView AdminHome(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account currentUser = (Account) session.getAttribute("CURRENT_USER");
        if (currentUser == null || !currentUser.isAccountType()) {
            return new ModelAndView(new RedirectView("/quanly.htm?view=login"));
        } else {
            return new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20"));
        }
    }

    @RequestMapping("/quanly")
    //public ModelAndView AdminRedirect(@RequestParam String view, HttpServletRequest request) {
    public ModelAndView AdminRedirect(@RequestParam Map<String, String> params, HttpServletRequest request) {
        String view = (String) params.get("view");
        HttpSession session = request.getSession();
        Account currentUser = (Account) session.getAttribute("CURRENT_USER");
        ModelAndView mav = new ModelAndView("pagenotfound");
        if (currentUser != null && currentUser.isAccountType()) {
            if (view == null || view.equals("login")) {
                mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20"));
            } else if (view.equals("logout")) {
                session.invalidate();
                mav = new ModelAndView(new RedirectView("/admin.htm"));
            } else if (view.equals("quanlysanpham")) {
                ProductDAO productDAO = new ProductDAO();
                String act = params.get("act");
                if (act == null) {
                    mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20"));
                } else if (act.equals("view")) {
                    if (params.get("page") == null || params.get("item") == null) {
                        mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20"));
                    } else {
                        int page = Integer.parseInt(params.get("page").toString());
                        int item = Integer.parseInt(params.get("item").toString());
                        List<Product> productList = productDAO.getProductList();
                        mav = new ModelAndView("admin/AdminMasterPage");
                        mav.addObject("CONTENT_PAGE", "/WEB-INF/jsp/admin/QuanLySanPham.jsp");
                        int nPage = FS_Ulti.PaginationTotalPage(productList, item);
                        try {
                            productList = (List<Product>) FS_Ulti.Pagination(productList, page, item);
                            mav.addObject("MESSAGE", "SUCCESS");
                            mav.addObject("MANUFACTURER_DAO", new ManufacturerDAO());
                            mav.addObject("CATEGORY_DAO", new CategoryDAO());
                            mav.addObject("CURRENT_PAGE", page);
                            mav.addObject("TOTAL_PAGE", nPage);
                            mav.addObject("ITEM_PER_PAGE", item);
                            mav.addObject("ITEM_LIST", productList);
                        } catch (Exception ex) {
                            mav.addObject("MESSAGE", ex.getMessage());
                            mav.addObject("ITEM_PER_PAGE", item);
                        }

                        String message = params.get("message");
                        if (message != null) {
                            mav.addObject("MESSAGE", message);
                        }
                    }
                } else if (act.equals("delete")) {
                    if (params.get("id") == null) {
                        mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20"));
                    } else {
                        int id = Integer.parseInt(params.get("id").toString());
                        try {
                            productDAO.deleteProduct(id);
                            mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20&message=PRODUCT_DELETE_SUCCESS"));
                        } catch (Exception ex) {
                            mav = new ModelAndView(new RedirectView("/quanly.htm?view=quanlysanpham&act=view&page=1&item=20&message=" + ex.getMessage()));
                        }
                    }
                } else if (act.equals("add")) {
                }
            } else if (view.equals("themsanpham")) {
                mav = new ModelAndView("admin/AdminMasterPage");
                mav.addObject("CONTENT_PAGE", "/WEB-INF/jsp/admin/ThemSanPham.jsp");
                List<Category> categoryList = CategoryDAO.getCategoryList();
                mav.addObject("CATEGORY_LIST", categoryList);
                List<Manufacturer> manufacturerList = ManufacturerDAO.getManufacturerList();
                mav.addObject("MANUFACTURER_LIST", manufacturerList);
            }
        } else {
            if (view == null || !view.equals("login")) {
                mav = new ModelAndView(new RedirectView("/quanly.htm?view=login"));
            } else if (view.equals("login")) {
                mav = new ModelAndView("admin/AdminLogin");
            }
        }
        return mav;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    String Login(@ModelAttribute(value = "mydata") LoginInfo mydata, BindingResult result, HttpServletRequest request) {
        String returnValue = "";
        HttpSession session = request.getSession();
        Account currentUser = (Account) session.getAttribute("CURRENT_USER");
        if (currentUser != null && currentUser.isAccountType()) {
            returnValue = "SUCCESS";
        } else {
            try {
                AccountDAO accountDAO = new AccountDAO();
                Account account = accountDAO.logIn(mydata.getUsername(), mydata.getPassword(), true);
                if (account != null) {
                    session.setAttribute("CURRENT_USER", account);
                    returnValue = "SUCCESS";
                }
            } catch (HibernateException ex) {
                Logger.getLogger(Admin.class
                        .getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                returnValue = ex.getMessage();
            }
        }
        return returnValue;
    }
    
}
